package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerNeedAdapter;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerMainOriginal.rCustomerMain;
import com.isunland.managesystem.entity.CustomerNeed;
import com.isunland.managesystem.entity.CustomerNeedOriginal;
import com.isunland.managesystem.entity.CustomerNeedQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 客户需求
 *
 * @author wangyanan
 */
public class CustomerNeedListFragment extends BaseListFragment {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";
  protected static final String TAG = "CustomerListFragment";
  private static final int MENU_ITEM_QUERY = 1;
  private static final int REQUEST_QUERY = 2;

  private ArrayList<CustomerNeed> mCustomerContacts;
  private CustomerNeedAdapter mAdapter;
  private rCustomerMain mCustomerDetail;
  private String mCustomerId;
  private CustomerNeedQueryParams mQueryParams;

  public static Fragment newInstance(rCustomerMain customerDetail) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, customerDetail);
    CustomerNeedListFragment fragment = new CustomerNeedListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.customer_need_list);
    mCustomerDetail = (rCustomerMain) getArguments().getSerializable(EXTRA_VALUE);
    mCustomerId = mCustomerDetail != null ? mCustomerDetail.getId() : "";
    //String customerName = mCustomerDetail != null ? mCustomerDetail.getCustomerName() : "";
    mQueryParams = new CustomerNeedQueryParams();
    mQueryParams.setStartDate(MyDateUtil.format(MyDateUtil.getBeforeMonth(1)));
    mQueryParams.setEndDate(MyDateUtil.format(new Date()));
    mQueryParams.setDateName("近一个月");
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_need_list, menu);
    menu.add(0, MENU_ITEM_QUERY, 0, R.string.query);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), CustomerNeedActivity.class);
        intent.putExtra(CustomerNeedFragment.EXTRA_TYPE, CustomerNeedFragment.VALUE_ADD);
        //			intent.putExtra(CustomerNeedFragment.CUSTOMER_NAME, customerName);
        //			intent.putExtra(CustomerNeedFragment.CUSTOMER_ID, mCustomerId);
        intent.putExtra(CustomerNeedFragment.CUSTOMERMAIN, mCustomerDetail);
        startActivityForResult(intent, 0);
        return true;
      case R.id.menu_item_productlist:
        Intent i = new Intent(getActivity(), ProductCatalogListActivity.class);
        startActivity(i);
        return true;

      case MENU_ITEM_QUERY:
        BaseVolleyActivity.newInstance(this, CustomerNeedQueryActivity.class, mQueryParams,
            REQUEST_QUERY);
        return true;

      //		case R.id.menu_item_estimate:
      //			Toast.makeText(getActivity(), R.string.product_estimate, Toast.LENGTH_SHORT).show();
      //			return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("customerId", mCustomerId);
    params.put("customerName", mQueryParams.getCustomerName());
    params.put("needProductDesc", mQueryParams.getCustomerNeed());
    params.put("beginnearContactDate", mQueryParams.getStartDate());
    params.put("endnearContactDate", mQueryParams.getEndDate());
    params.put("memberCode", CurrentUser.newInstance(mActivity).getMemberCode());
    params.put("orderField", "REG_DATE");
    params.put("orderSeq", "desc");
    return params.getParams();
  }

  @Override public String getUrl() {

    return ApiConst.URL_CUSTOMER_NEED_LIST;
  }

  @Override public void resolveResponse(String arg0) {

    CustomerNeedOriginal customerContactOriginal =
        new Gson().fromJson(arg0, CustomerNeedOriginal.class);

    List<CustomerNeed> customerContacts = customerContactOriginal.getRows();

    setAdapter(customerContacts);
  }

  private void setAdapter(List<CustomerNeed> customerContacts) {
    if (mCustomerContacts == null) {
      mCustomerContacts = new ArrayList<>();
    }
    mCustomerContacts.clear();
    mCustomerContacts.addAll(customerContacts);

    if (mAdapter == null) {
      mAdapter = new CustomerNeedAdapter(getActivity(), mCustomerContacts);
      setListAdapter(mAdapter);
    }
    ((CustomerNeedAdapter) getListAdapter()).notifyDataSetChanged();
    // MyUtils.dismissDialog();
  }

  public void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(getActivity(), CustomerPagerActivity.class);
    CustomerNeed customerNeed = ((CustomerNeedAdapter) getListAdapter()).getItem(position - 1);
    i.putExtra(CustomerNeedFragment.EXTRA_CUSTOMER_NEED, customerNeed);
    i.putExtra(CustomerNeedFragment.EXTRA_TYPE, CustomerNeedFragment.VALUE_SHOW);

    startActivityForResult(i, 0);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY) {
      mQueryParams = (CustomerNeedQueryParams) data.getSerializableExtra(BaseFragment.EXTRA_PARAMS);
    }

    volleyPost();

    super.onActivityResult(requestCode, resultCode, data);
  }
}
